﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UML2DOC.Interfaces;
using System.IO;

namespace UML2DOC.ModelProjectEnumerator
{
    public class Solution: ISolution
    {
        private string _filename;
  
        private Solution(string fileName)
        {
            _filename = fileName;
            SetProperties();
        }

        public static Solution GetSolution(string fileName)
        {
            return new Solution(fileName);
        }

        private void SetProperties()
        {

            _modelProjects.Clear();
            FileInfo fi = new FileInfo(_filename);
            _name = fi.FullName;

            Type type = Type.GetTypeFromProgID("VisualStudio.DTE");
            EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(type);

            dte.Solution.Open(_name);

            List<string> modelProjects = new List<string>();
            IModelProject modelProject;

            foreach (EnvDTE.Project project in dte.Solution.Projects)
            {
                if (String.Compare(project.Kind, "{F088123C-0E9E-452A-89E6-6BA2F21D5CAC}", true) == 0)
                {

                    modelProject = new ModelProject(project.Name, project.ProjectItems);
                    _modelProjects.Add(modelProject);

                }
            }

            dte.Quit();

        }

        private IList<IModelProject> _modelProjects = new List<IModelProject>();
        public IList<IModelProject> ModelProjects
        {
            get
            {
                return _modelProjects;
            }
            set
            {
               throw new NotImplementedException();
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name= value;
            }
        }
    }
}
